//
//  LSExpertRankViewController.m
//  LookScore
//
//  Created by wws on 2018/5/3.
//  Copyright © 2018年 ChongQingXunTiCompany. All ·· reserved.
//

#import "LSExpertRankViewController.h"
#import "UIViewController+ZJScrollPageController.h"
#import "LSExpertRankCell.h"
#import "LSExpertRankModel.h"
@interface LSExpertRankViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property(strong,nonatomic)NSArray *expertRankList;
@property (nonatomic, copy) NSString * tokenStr;
@property (nonatomic, assign) NSInteger type;
@end

@implementation LSExpertRankViewController{
    BOOL selected;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    selected = NO;
    self.view.backgroundColor = [UIColor whiteColor];
}

-(void)zj_viewDidLoadForIndex:(NSInteger)index{
    [notifiCenter addObserver:self selector:@selector(updateData) name:@"LOADCIRCLEDATA" object:nil];
    [self.view addSubview:self.tableView];
    self.type = index;
    self.tableView.sd_layout
    .leftEqualToView(self.view)
    .topEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomEqualToView(self.view).offset(-64);
    if ([LSAccountArchive isLogin]) {
        _tokenStr = [LSAccountArchive account].Cid;
    }else{
        _tokenStr = @"";
    }
    [self loadExpertRankDataWith:index];
}
- (void)updateData{
    [self loadExpertRankDataWith:self.type];
}
- (void)loadExpertRankDataWith:(NSInteger)type{
    NSMutableArray * rankListArray = @[].mutableCopy;
    NSMutableArray * recommentArray = @[].mutableCopy;
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
//    hud.label.text=@"正在请求中...";
    hud.mode=MBProgressHUDModeIndeterminate;
    //    [hud showAnimated:YES];
    [LSHttpEnclosureTool GET:LSExpertRankList parameters:@{@"type":@(type),@"cid":_tokenStr} resBlock:^(id responseObject) {
        if (responseObject) {
            for (NSDictionary * dict in responseObject[@"RankList"]) {
                
                LSExpertRankModel * model = [LSExpertRankModel mj_objectWithKeyValues:dict];
                model.type = type;
                [rankListArray addObject:model];
            }
            
            for (NSDictionary * dict in responseObject[@"RecommendList"]) {
                LSRecommendListModel * model = [LSRecommendListModel mj_objectWithKeyValues:dict];
                [recommentArray addObject:model];
            }
            [hud hideAnimated:YES];
            self.dataArray = @[rankListArray,recommentArray].mutableCopy;
            MyLog(@"leagueTable ---- %@",responseObject);
        }
        
        [self.tableView reloadData];
    }];
}

#pragma mark 🔥 tableViewDelegate tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        NSArray * array = self.dataArray[section];
        if (array.count > 5) {
            if (selected == NO) {
                
                return 5;
            }
            return array.count;
        }else{
            return array.count;
        }
        
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        if (selected == NO) {
            return 30;
        }
    }
    return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray * array = self.dataArray[indexPath.section];
    if (indexPath.section==0) {
        return 63;
    }

    return (LSScreenWidth/4 + 20) * (array.count % 4);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 35;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, LSAutoVlaue(45, LSScreenHeight))];
    headerView.backgroundColor = LSBackGroundColor;
    
    if (section == 1) {
        UIView *whiteView = [[UIView alloc] init];
        whiteView.backgroundColor = [UIColor whiteColor];
        [headerView addSubview:whiteView]; whiteView.sd_layout.leftEqualToView(headerView).topEqualToView(headerView).offset(LSAutoVlaue(9, LSScreenHeight)).rightEqualToView(headerView).bottomEqualToView(headerView);
        
        UILabel *titleLable = [[UILabel alloc] init];
        titleLable.text = @"推荐专家";
        titleLable.font = LSFontSize13;
        titleLable.textColor = LSSubColore5735e;
        [headerView addSubview:titleLable];
        titleLable.sd_layout.leftEqualToView(whiteView).offset(LSAutoVlaue(30, LSScreenWidth)).centerYEqualToView(whiteView).widthIs(LSAutoVlaue(80, LSScreenWidth)).heightIs(LSAutoVlaue(26, LSScreenHeight));
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = LSSubColore5735e;
        [headerView addSubview:line];
        line.sd_layout.leftEqualToView(whiteView).offset(LSAutoVlaue(20, LSScreenWidth)).centerYEqualToView(titleLable).widthIs(2).heightIs(LSAutoVlaue(15, LSScreenHeight));
    }
    return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        if (selected == NO) {
            UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 30)];
            footer.backgroundColor = LSTitleWhiteColor;
            UIButton *moreRankBtn = [[UIButton alloc] init];
            [moreRankBtn setTitle:@"更多排行" forState:UIControlStateNormal];
            [moreRankBtn setTitleColor:LSNavMainColor forState:UIControlStateNormal];
            [moreRankBtn setImage:[UIImage imageNamed:@"MoreRank"] forState:UIControlStateNormal];
            [moreRankBtn addTarget:self action:@selector(clickMoreRankBtn:) forControlEvents:UIControlEventTouchUpInside];
            moreRankBtn.titleLabel.font = LSFontSize(14, LSScreenWidth);
            [footer addSubview:moreRankBtn];
            moreRankBtn.sd_layout
            .centerYEqualToView(footer)
            .rightEqualToView(footer).offset(-LSAutoVlaue(10, LSScreenWidth))
            .widthIs(LSAutoVlaue(100, LSScreenWidth))
            .heightIs(LSAutoVlaue(30, LSScreenHeight));
            
            return footer;
        }else{
            return nil;
        }
    }
    return nil;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        NSArray *array = @[@"Rank1",@"Rank2",@"Rank3",@"Rank4",@"Rank5",@"Rank6",@"Rank7",@"Rank8",@"Rank9",@"Rank10"];
        
        LSExpertRankCell *cell=[LSExpertRankCell ExpertRankCellCellByTableView:tableView];
        cell.rankImageView.image = [UIImage imageNamed:array[indexPath.row]];
        NSArray * modelArray = self.dataArray[indexPath.section];
        cell.model = modelArray[indexPath.row];
        cell.clickFollowButtonBlock = ^(NSString *expertCid) {
            MyLog(@"关注 cid--- %@",expertCid);
            [self followExpertsWithCid:expertCid];
        };
        return cell;
    }else{
        LSRecommendExpertCell *cell=[LSRecommendExpertCell RecommendExpertCelllCellByTableView:tableView];
        cell.dataArray = self.dataArray[indexPath.section];
        cell.clickFollowButtonBlock = ^(NSString *expertCid) {
            MyLog(@"关注 cid--- %@",expertCid);
            [self followExpertsWithCid:expertCid];
        };
        return cell;
    }
    
    
}

#pragma mark 🔥 Button Target
- (void)clickMoreRankBtn:(UIButton *)sender{
//    sender.selected = !sender.selected;
    selected = YES;
    [self.tableView reloadData];
    MyLog(@"更多排行");
}

- (void)followExpertsWithCid:(NSString *)Cid{
    if (![LSAccountArchive isLogin]) {
        LSLoginVController *loginVC=[[LSLoginVController alloc]init];
        [self presentRootViewController:loginVC animated:YES completion:nil];
        loginVC.loginSuccessBlock = ^{
//            [self.tableView reloadData];
            _tokenStr = [LSAccountArchive account].Cid;
            [self loadExpertRankDataWith:self.type];
            
        };
        
    }else{
        [LSHttpEnclosureTool POST:LSUserAttention parameters:@{@"fanCid":[LSAccountArchive account].Cid,@"Cid":Cid} resBlock:^(id responseObject) {
            if (responseObject) {
                MyLog(@"responseObject --- %@",responseObject);
                [notifiCenter postNotificationName:@"LOADCIRCLEDATA" object:nil];

                [self loadExpertRankDataWith:self.type];
            }
        }];
    }
}
#pragma mark 🔥 Getter
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (NSArray *)expertRankList{
    if (!_expertRankList) {
        _expertRankList = @[].mutableCopy;
    }
    return _expertRankList;
}
@end




